package org.bonorsoft.modules.control.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.bonorsoft.modules.control.entity.Template;
import org.bonorsoft.modules.control.mapper.TemplateMapper;
import org.bonorsoft.modules.control.service.ITemplateService;
import org.bonorsoft.modules.control.util.WordToHtml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 检查模板
 * @Author: WilliamYe
 * @Date:   2021-02-02
 * @Version: V1.0
 */
@Service
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template> implements ITemplateService {


    @Autowired
    WordToHtml wordToHtml;

    @Override
    public boolean save(Template template) {
        JSONObject json = wordToHtml.wordToHtml(template.getTemplatePath());
        if(json.get("result").toString().equals("true")){
            String html = json.get("list").toString();
            String newHtml = handleHtml(html);
            template.setTemplateHtml(newHtml);
            return retBool(baseMapper.insert(template));
        }else {
            return false;
        }
    }

    @Override
    public boolean updateById(Template template) {
        JSONObject json = wordToHtml.wordToHtml(template.getTemplatePath());
        if(json.get("result").toString().equals("true")){
            String html = json.get("list").toString();
            String newHtml = handleHtml(html);
            template.setTemplateHtml(newHtml);
            return retBool(baseMapper.updateById(template));
        }else {
            return false;
        }
    }

    /**
     * 处理html内容（□换成复选框）
     * @param html
     * @return
     */
    private String handleHtml(String html){
        return html.replaceAll("□", "<input type=\"checkbox\" value=\"\"/>");
    }

}
